package cn.tedu._05weibo3.controller;

import cn.tedu._05weibo3.common.response.JsonResult;
import cn.tedu._05weibo3.common.response.StatusCode;
import cn.tedu._05weibo3.mapper.UserMapper;
import cn.tedu._05weibo3.pojo.dto.UserLoginDTO;
import cn.tedu._05weibo3.pojo.dto.UserRegDTO;
import cn.tedu._05weibo3.pojo.entity.User;
import cn.tedu._05weibo3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v1/user/")
@Api(tags = "01.用户管理模块")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping("register")
    @ApiOperation("用户注册")
    public JsonResult register(@RequestBody @Validated UserRegDTO userRegDTO, @ApiIgnore HttpSession session) {
        log.debug("开始处理注册请求，参数：{}", userRegDTO);
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO != null) {
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        } else {
            User user = new User();
            BeanUtils.copyProperties(userRegDTO, user);
            user.setCreated(new Date());
            log.debug("即将插入的数据：{}", user);
            userMapper.insertUser(user);
//            session.setAttribute("user", userVO);
//            log.debug("用户信息：{}", userVO);
            return JsonResult.ok();
        }
    }

    @PostMapping("login")
    @ApiOperation("用户登录")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        log.debug("开始处理登录请求，参数：{}", userLoginDTO);
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_ERROR);
        } else if (!userVO.getPassword().equals(userLoginDTO.getPassword())) {
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        } else {
            session.setAttribute("user", userVO);
            log.debug("用户信息：{}", userVO);
            return JsonResult.ok();
        }
    }

    @GetMapping("currentUser")
    @ApiOperation("获取当前登录用户")
    public JsonResult currentUser(@ApiIgnore HttpSession session) {
        log.debug("开始处理获取当前登录用户的请求");
//        if (session.getAttribute("user") == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }

    @GetMapping("logout")
    @ApiOperation("退出登录")
    public JsonResult logout(@ApiIgnore HttpSession session) {
        log.debug("开始处理退出登录请求");
        session.removeAttribute("user");
        return JsonResult.ok();
    }

}
